package com.lebang.controller.sys;

import com.lebang.common.Login;
import com.lebang.domain.TaskRouse;
import com.lebang.example.DownloadMemberRecordExample;
import com.lebang.feign.HundredAwardsFeign;
import com.lebang.model.RequestUserInfo;
import com.lebang.service.DownloadMemberRecordService;
import com.lebang.service.NewRouseService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 每日必赚记录
 *
 * @author hwq
 * @date 2020-01-13
 */
@Controller
@RequestMapping("/sys/busdownloadmemberrecord")
public class BusDownloadMemberRecordController {
    @Autowired
    private DownloadMemberRecordService baseService;
    @Autowired
    private NewRouseService newRouseService;
    @Autowired
    private HundredAwardsFeign hundredAwardsFeign;
    @Value("#{'${check.sdkList}'.split(',')}")
    private List<String> sdkList;


    @Login
    @GetMapping("/list")
    public String list(HttpServletRequest request,Model model, String pkgName, HttpSession session) {
        if(!TextUtil.isEmpty(pkgName)){
            model.addAttribute("pkgName", pkgName);
        }
        RequestUserInfo requestUserInfo=new RequestUserInfo(request);
        JsonData data = hundredAwardsFeign.doubleConfig(requestUserInfo);
        model.addAttribute("doubleConfig",data.getData());
        List<TaskRouse> rouses = newRouseService.selectAll();
        rouses = newRouseService.checkRoot(rouses,session);
        model.addAttribute("item", rouses);
        return "/busdownloadmemberrecord/list";
    }


    @Login
    @PostMapping("/data")
    @ResponseBody
    public Map<String, Object> datalist(HttpSession session
            , @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime start
            , @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime end
            , @RequestParam(required = false) String pkgName
            , @RequestParam(required = false) String memberId
            , @RequestParam(required = false) String sdkKey
            , @RequestParam(required = false) Integer status
            , @RequestParam(required = false) Integer type
            , @RequestParam(required = false) String appStore
            , @RequestParam(required = false) Integer downType
            , @RequestParam(required = false) Integer isNewMember
            ,Long page, Integer limit) {
        DownloadMemberRecordExample example = new DownloadMemberRecordExample();
        example.setOrderByClause("create_time desc");
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        DownloadMemberRecordExample.Criteria criteria = example.createCriteria();
        if(newRouseService.isRoot(session)){
            criteria.andSdkKeyNotIn(sdkList);
        }
        if(!TextUtil.isEmpty(start)&&!TextUtil.isEmpty(end)){
            criteria.andCreateTimeBetween(start,end);
        }
        if(!TextUtil.isEmpty(pkgName)){
            criteria.andPkgNameEqualTo(pkgName);
        }
        if(!TextUtil.isEmpty(memberId)){
            criteria.andMemberIdEqualTo(memberId);
        }
        if(!TextUtil.isEmpty(sdkKey)){
            criteria.andSdkKeyEqualTo(sdkKey);
        }
        if(!TextUtil.isEmpty(status)){
            criteria.andStatusEqualTo(status);
        }
        if(!TextUtil.isEmpty(type)){
            criteria.andTypeEqualTo(type);
        }
        if (!TextUtil.isEmpty(appStore)){
            criteria.andAppStoreEqualTo(appStore);
        }
        if(!TextUtil.isEmpty(downType)){
            criteria.andDownTypeEqualTo(downType);
        }
        if(!TextUtil.isEmpty(isNewMember)){
            criteria.andIsNewMemberEqualTo(isNewMember);
        }
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("data", baseService.selectByExample(example));
        returnMap.put("count", baseService.countByExample(example));
        returnMap.put("code", 0);
        return returnMap;

    }

}
